package org.opens.protobuf.test;

import cn.hutool.core.util.IdUtil;
import com.google.protobuf.ByteString;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.opens.netty.chatServer.message.*;
import org.opens.protobuf.MessageProto;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * 功能描述
 *
 * @author: zhangyuming
 */
public class MessageProtoMyMessageHandle extends SimpleChannelInboundHandler<MessageProto.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProto.MyMessage msg) throws Exception {
        System.out.println("1消息是: " + msg + "结束");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        waitKeyboardInput(ctx);
    }

    public void waitKeyboardInput(ChannelHandlerContext ctx) {
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                // 打印菜单, 提供行为选择
                System.out.println("====================================");
                System.out.println("输入 1 发送MyMessage");
                System.out.println("输入 2 发送NetworkAccess");
                System.out.println("输入 3 发送WavMessage");
                System.out.println("输入 quit 终止");

                String command = scanner.nextLine();

                switch (command) {
                    case "1":
                        sendMyMessage(ctx);
                        break;
                    case "2":
                        sendNetworkAccess(ctx);
                        break;
                    case "3":
                        sendWav(ctx);
                        break;
                    case "quit":
                        ctx.channel().close();
                        break;
                    default:
                        break;
                }

            }
        }, "waitSystemIn").start();
    }

    public void sendWav(ChannelHandlerContext ctx) {
        try {
            int CHUNK_SIZE = 1024;
            byte[] audioData = Files.readAllBytes(Paths.get("C:\\Users\\99126\\Desktop\\mp3\\13202.wav"));
            int totalChunks = (int) Math.ceil((double) audioData.length / CHUNK_SIZE);

            String uuid = IdUtil.fastSimpleUUID();

            for (int i = 0; i < totalChunks; i++) {
                int start = i * CHUNK_SIZE;
                int end = Math.min(start + CHUNK_SIZE, audioData.length);

                byte[] chunkData = new byte[end - start];
                System.arraycopy(audioData, start, chunkData, 0, end - start);

                MessageProto.WavMessage wavMessage = MessageProto.WavMessage.newBuilder()
                        .setWavUuid(uuid)
                        .setTotal(totalChunks)
                        .setCurrent(i + 1)
                        .setData(ByteString.copyFrom(chunkData))
                        .build();

                MessageProto.MessageWrapper messageWrapper = MessageProto.MessageWrapper.newBuilder()
                        .setVersion(MessageProto.Version.v1)
                        .setType(MessageProto.MessageType.WAV_MESSAGE)
                        .setWavMessage(wavMessage)
                        .build();

                ctx.writeAndFlush(messageWrapper);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void sendMyMessage(ChannelHandlerContext ctx) {
        MessageProto.MyMessage message = MessageProto.MyMessage.newBuilder()
                .setId(14)
                .setContent("ni hao Hello, Netty!")
                .build();
        MessageProto.MessageWrapper messageWrapper = MessageProto.MessageWrapper.newBuilder()
                .setVersion(MessageProto.Version.v1)
                .setType(MessageProto.MessageType.MY_MESSAGE)
                .setMyMessage(message)
                .build();
        ctx.writeAndFlush(messageWrapper);
    }

    public void sendNetworkAccess(ChannelHandlerContext ctx) {
        MessageProto.NetworkAccess networkAccess = MessageProto.NetworkAccess.newBuilder()
                .setField1(ByteString.copyFrom(new byte[]{0x01, 0x02}))
                .setField2(ByteString.copyFrom(new byte[]{0x03, 0x04}))
                .setField3(ByteString.copyFrom(new byte[]{0x05, 0x06}))
                .setField4(ByteString.copyFrom(new byte[]{0x07, 0x08}))
                .setField5(ByteString.copyFrom(new byte[]{0x09, 0x0A}))
                .setField6(ByteString.copyFrom(new byte[]{0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}))
                .setField7(ByteString.copyFrom(new byte[]{0x11, 0x12}))
                .setField8(ByteString.copyFrom(new byte[]{0x13, 0x14}))
                .setField9(ByteString.copyFrom(new byte[]{0x15, 0x16}))
                .build();
        MessageProto.MessageWrapper networkAccessWrapper = MessageProto.MessageWrapper.newBuilder()
                .setVersion(MessageProto.Version.v1)
                .setType(MessageProto.MessageType.NETWORK_ACCESS)
                .setNetworkAccess(networkAccess)
                .build();
        ctx.writeAndFlush(networkAccessWrapper);
    }

}
